package com.bxt.demo.price;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正式的程序需要通过修改正则表达式增加通配符号，以适应更多促销信息，其中多级促销信息，需要按照数据顺序从多到少进行处理
 * 这样通过穷举，最终可以满足多数数据的清洗需求，极个别无法清洗数据，最后只能手动纠正。
 */
public class RegExp {
    //计算方式
    static String exp1 = "购买至少(\\d+)件时可享受优惠,满(\\d+)元减(\\d+)元";
    static String exp2 = "满(\\d+)件,加赠超值赠品（赠完即止）,淘金币可抵125.97元起,商品券满(\\d+)减(\\d+),满1件送赠品,购买得积分";
    static String exp3 = "购买1-(\\d+)件时享受单件价¥229，超出数量以结算价为准,满(\\d+)减(\\d+)";
    static String exp4 = "满(\\d+)减(\\d+),满(\\d+)减(\\d+),满499减(\\d+),满(\\d+)减(\\d+),满(\\d+)减(\\d+)";
    static String exp5 = "满(\\d+)元,省(\\d+)元";
    static String exp6 = "每(\\d+)减(\\d+),可跨店,上不封顶";
    static String exp7 = "每满(\\d+)元,省(\\d+)元";
    static String exp8 = "每满(\\d+)元，可减(\\d+)元现金，最多可减(\\d+)元";
    static String exp9 = "每满(\\d+)元，可减(\\d+)元现金,每满(\\d+)元，可减(\\d+)元现金";
    static String exp10 = "下单再享(\\d+(\\.\\d+)?)折";
    static String exp11 = "满(\\d+)件,打(\\d+(\\.\\d+)?)折";
    static String exp12 = "满(\\d+)享(\\d+(\\.\\d+)?)折";
    static String exp13 = "满(\\d+)件，总价打(\\d+(\\.\\d+)?)折；满3件，总价打(\\d+(\\.\\d+)?)折";
    static String exp14 = "满(\\d+)元,享部分地区包邮,下单再享(\\d+(\\.\\d+)?)折";
    static String exp15 = "每(\\d+)减(\\d+),可跨店,上不封顶,满(\\d+)件,打(\\d+(\\.\\d+)?)折";
    static String exp16 = "满(\\d+)元减(\\d+)元，满(\\d+)元减(\\d+)元，满(\\d+)元减(\\d+)元,满(\\d+)件，总价打(\\d+(\\.\\d+)?)折；满(\\d+)件，总价打(\\d+(\\.\\d+)?)折";
    static String exp17 = "满(\\d+)件,打(\\d+(\\.\\d+)?)折,满(\\d+)元,享部分地区包邮,全球探物券满1800减240,满1件(\\d+(\\.\\d+)?)折";

    public static float match1(int price,String str){
        Pattern regex = Pattern.compile(exp1);
        // 创建 Matcher 对象
        Matcher matcher = regex.matcher(str);
        // 匹配和提取信息
        if (matcher.matches()) {
            int quantity = Integer.parseInt(matcher.group(1));
            int totalAmount = Integer.parseInt(matcher.group(2));
            float discountAmount = Float.parseFloat(matcher.group(3));
            if (totalAmount > price) {
                int m = totalAmount/price;
                m++;
                return (price*m-discountAmount)/m;
            } else {
                return price - discountAmount;
            }
        } else {
            return -1;
        }
    }

    public static float match2(int price,String str){
        Pattern regex = Pattern.compile(exp1);
        // 创建 Matcher 对象
        Matcher matcher = regex.matcher(str);
        // 匹配和提取信息
        if (matcher.matches()) {
            int quantity = Integer.parseInt(matcher.group(1));
            int totalAmount = Integer.parseInt(matcher.group(2));
            float discountAmount = Float.parseFloat(matcher.group(3));
            System.out.println("购买数量：" + quantity);
            System.out.println("满减条件金额：" + totalAmount);
            System.out.println("满减金额：" + discountAmount);
            if (totalAmount > price) {
                int m = totalAmount/price;
                m++;
                return (price*m-discountAmount)/m;
            } else {
                return price - discountAmount;
            }
        } else {
            return -1;
        }
    }


    public static void main(String[] args) {
        System.out.println("R:"+match1(1169,"购买至少1件时可享受优惠,满1249元减130元"));
    }




}
